package com.example.crmdemo.Crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.crmdemo.Crm.entity.Area;
import com.example.crmdemo.Crm.entity.Person;
import com.example.crmdemo.Crm.service.AreaService;
import com.example.crmdemo.Crm.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @Autowired
    private AreaService areaService;

    // 添加人员
    @PostMapping("/add")
    public boolean addPerson(@RequestBody Person person) {
        return personService.save(person);
    }

    // 删除人员
    @DeleteMapping("/delete/{id}")
    public boolean deletePerson(@PathVariable Long id) {
        return personService.removeById(id);
    }

    // 修改人员信息
    @PutMapping("/update")
    public boolean updatePerson(@RequestBody Person person) {
        return personService.updateById(person);
    }

    // 按人员名查询人员
    @GetMapping("/get/{name}")
    public List<Person> getPersonByName(@PathVariable String name) {
        QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p_name", name);
        return personService.list(queryWrapper);
    }

    // 获取所有人员信息
    @GetMapping("/getall")
    public List<Person> getAllPersons() {
        return personService.list();
    }

    // 获取所有区域信息
    @GetMapping("/areas")
    public List<Area> getAllAreas() {
        return areaService.list();
    }
}
